cmake_minimum_required(VERSION 3.22)
project(upnp C)

include_directories(  
    "libupnp"  
    "libupnp/ixml/inc"  
    "libupnp/ixml/src/inc"  
    "libupnp/upnp/inc"  
    "libupnp/upnp/src/inc"  
    "libupnp/upnp/src/threadutil"  
)  

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-format -Wno-shadow -Wno-unused-parameter -Wno-unused-function -Wno-missing-field-initializers -Wno-non-pod-varargs -Wno-writable-strings -Wno-unused-but-set-variable -Wno-unused-variable -Wno-conversion -FS -O2 -D_FORTIFY_SOURCE=2 -fvisibility=hidden -fvisibility-inlines-hidden")  

set(IXML_SOURCES
	libupnp/ixml/src/attr.c
    	libupnp/ixml/src/document.c
    	libupnp/ixml/src/element.c
    	libupnp/ixml/src/ixml.c
    	libupnp/ixml/src/ixmldebug.c
    	libupnp/ixml/src/ixmlparser.c
    	libupnp/ixml/src/ixmlmembuf.c
    	libupnp/ixml/src/namedNodeMap.c
    	libupnp/ixml/src/node.c
    	libupnp/ixml/src/nodeList.c
)

add_library(ixml STATIC ${IXML_SOURCES})

set(UPNP_SOURCES
	libupnp/upnp/src/threadutil/FreeList.c
    	libupnp/upnp/src/threadutil/LinkedList.c
    	libupnp/upnp/src/threadutil/ThreadPool.c
    	libupnp/upnp/src/threadutil/TimerThread.c
    	#ssdp
    	libupnp/upnp/src/ssdp/SSDPResultData.c
    	libupnp/upnp/src/ssdp/SSDPResultDataCallback.c
    	libupnp/upnp/src/ssdp/ssdp_device.c
    	libupnp/upnp/src/ssdp/ssdp_ctrlpt.c
    	libupnp/upnp/src/ssdp/ssdp_server.c
    	#soap
    	libupnp/upnp/src/soap/soap_device.c
    	libupnp/upnp/src/soap/soap_ctrlpt.c
    	libupnp/upnp/src/soap/soap_common.c
    	#genlib
    	libupnp/upnp/src/genlib/miniserver/miniserver.c
    	libupnp/upnp/src/genlib/client_table/client_table.c
    	libupnp/upnp/src/genlib/client_table/GenlibClientSubscription.c
    	libupnp/upnp/src/genlib/service_table/service_table.c
    	libupnp/upnp/src/genlib/util/membuffer.c
    	libupnp/upnp/src/genlib/util/strintmap.c
    	libupnp/upnp/src/genlib/util/util.c
    	libupnp/upnp/src/genlib/util/list.c
    	libupnp/upnp/src/genlib/net/sock.c
    	libupnp/upnp/src/genlib/net/http/httpparser.c
    	libupnp/upnp/src/genlib/net/http/httpreadwrite.c
    	libupnp/upnp/src/genlib/net/http/statcodes.c
    	libupnp/upnp/src/genlib/net/http/webserver.c
    	libupnp/upnp/src/genlib/net/http/parsetools.c
    	libupnp/upnp/src/genlib/net/uri/uri.c
    	#gena
    	libupnp/upnp/src/gena/gena_device.c
    	libupnp/upnp/src/gena/gena_ctrlpt.c
    	libupnp/upnp/src/gena/gena_callback2.c
    	#api
    	libupnp/upnp/src/api/UpnpActionComplete.c
    	libupnp/upnp/src/api/UpnpActionRequest.c
    	libupnp/upnp/src/api/UpnpDiscovery.c
    	libupnp/upnp/src/api/UpnpEvent.c
    	libupnp/upnp/src/api/UpnpEventSubscribe.c
    	libupnp/upnp/src/api/UpnpExtraHeaders.c
    	libupnp/upnp/src/api/UpnpFileInfo.c
    	libupnp/upnp/src/api/UpnpStateVarComplete.c
    	libupnp/upnp/src/api/UpnpStateVarRequest.c
    	libupnp/upnp/src/api/UpnpSubscriptionRequest.c
    	libupnp/upnp/src/api/UpnpString.c
    	libupnp/upnp/src/api/upnpapi.c
    	libupnp/upnp/src/api/upnpdebug.c
    	libupnp/upnp/src/api/upnptools.c
    	#uuid
    	libupnp/upnp/src/uuid/md5.c
    	libupnp/upnp/src/uuid/sysdep.c
    	libupnp/upnp/src/uuid/uuid.c
    	#urlconfig
    	libupnp/upnp/src/urlconfig/urlconfig.c
)

add_library(upnp SHARED ${UPNP_SOURCES})

target_link_libraries(upnp PRIVATE ixml)

add_custom_command(TARGET upnp POST_BUILD
	COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_INSTALL_PREFIX}/lib
	COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_INSTALL_PREFIX}/include/upnp
	COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:upnp> ${CMAKE_INSTALL_PREFIX}/lib/
	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/libupnp/autoconfig.h ${CMAKE_INSTALL_PREFIX}/include/upnp/
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/libupnp/ixml/inc/ ${CMAKE_INSTALL_PREFIX}/include/upnp/
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/libupnp/ixml/src/inc/ ${CMAKE_INSTALL_PREFIX}/include/upnp/
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/libupnp/upnp/inc/ ${CMAKE_INSTALL_PREFIX}/include/upnp/
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/libupnp/upnp/src/inc/ ${CMAKE_INSTALL_PREFIX}/include/upnp/
	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/libupnp/upnp/src/threadutil/FreeList.h ${CMAKE_INSTALL_PREFIX}/include/upnp/
	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/libupnp/upnp/src/threadutil/LinkedList.h ${CMAKE_INSTALL_PREFIX}/include/upnp/
	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/libupnp/upnp/src/threadutil/ThreadPool.h ${CMAKE_INSTALL_PREFIX}/include/upnp/
	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/libupnp/upnp/src/threadutil/TimerThread.h ${CMAKE_INSTALL_PREFIX}/include/upnp/
)
